package com.zrrd._01_servlet.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author WanJl
 * @version 1.0
 * @title RequestDemo03
 * @description
 * @create 2024/1/13 10:37
 */
@WebServlet("/RequestDemo04")
public class RequestDemo04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取post请求的请求体,并将请求体里的参数封装成Map集合
        Map<String, String[]> parameterMap = req.getParameterMap();
        //通过map的keySet方法获取key（键）的集合
        Set<String> set = parameterMap.keySet();
        //将集合获取迭代器对象
        Iterator<String> iterator = set.iterator();
        //循环迭代器
        while (iterator.hasNext()){
            //获取下一个key元素
            String key = iterator.next();
            //通过key作为参数，获取到value
            String[] value = parameterMap.get(key);
            System.out.println(key);
            System.out.println(Arrays.toString(value));
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
